<?
	$page["title"] = "Status of triggers";
	$page["file"] = "tr_status.html";

	include "include/config.inc";
	if(isset($fullscreen))
	{
		show_header($page["title"],10,1);
	}
	else
	{
		show_header($page["title"],10,0);
	}
?>                                                                                                             

<?
 
	if(!isset($onlytrue))
	{
		$onlytrue='false';
	}
	if($noactions!='true')
	{
		$noactions='false';
	}
	if($compact!='true')
	{
		$compact='false';
	}
?>

<?


	if(!isset($fullscreen))
	{
		show_table_header_begin();
		echo "STATUS OF TRIGGERS";
	        show_table_v_delimiter();

	        $result=DBselect("select hostid,host from hosts order by host");
	        if(isset($hostid))
	        {
	                echo "<A HREF=\"tr_status.html?onlytrue=$onlytrue&noactions=$noactions&compact=$compact\">all</A>  ";
	        }
	        else
	        {
	                echo "<b>[<A HREF=\"tr_status.html?onlytrue=$onlytrue&noactions=$noactions&compact=$compact\">all</A>]</b>  ";
	        }
		for($i=0;$i<DBnum_rows($result);$i++)
	        {
	                $hid=DBget_field($result,$i,0);
	                $host=DBget_field($result,$i,1);
	                if(isset($hostid) && ($hid == $hostid))
	                {
	                        echo "<b>[<A HREF=\"tr_status.html?hostid=$hid&onlytrue=$onlytrue&noactions=$noactions&compact=$compact\">$host</A>]</b>  ";
	                }
	                else
	                {
	                        echo "<A HREF=\"tr_status.html?hostid=$hid&onlytrue=$onlytrue&noactions=$noactions&compact=$compact\">$host</A>  ";
	                }
	        }
	 
		show_table_v_delimiter();
?>

<?
		if(isset($hostid))
		{
			$cond="&hostid=$hostid";
		}
		else
		{
			$cond="";
		}

		if($onlytrue!='true')
		{
			echo "[<A HREF=\"tr_status.html?onlytrue=true&noactions=$noactions&compact=$compact$cond\">Show only true</a>] ";
		}
		else
		{
			echo "[<A HREF=\"tr_status.html?noactions=$noactions&compact=$compact$cond\">Show all triggers</A>] ";
		}
		if($noactions!='true')
		{
			echo "[<A HREF=\"tr_status.html?onlytrue=$onlytrue&noactions=true&compact=$compact$cond\">Hide Actions</A>] ";
		}
		else
		{
			echo "[<A HREF=\"tr_status.html?onlytrue=$onlytrue&noactions=false&compact=$compact$cond\">Show Actions</A>] ";
		}
		if($compact!='true')
		{
			echo "[<A HREF=\"tr_status.html?onlytrue=$onlytrue&noactions=$noactions&compact=true$cond\">Hide Details</A>] ";
		}
		else
		{
			echo "[<A HREF=\"tr_status.html?onlytrue=$onlytrue&noactions=$noactions&compact=false$cond\">Show Details</A>] ";
		}
  
		show_table_header_end();
		echo "<br>";
	}
	
 
 	$time=date("[H:i:s]",time());
  	if(isset($fullscreen))
	{
		show_table_header("<A HREF=\"tr_status.html?onlytrue=$onlytrue&noactions=$noactions&compact=$compact\">TRIGGERS $time</A>");

		$cond="";
		if(isset($hostid))
		{
			$cond=" and h.hostid=$hostid ";
		}
		if($onlytrue=='true')
		{
			$result=DBselect("select t.priority,count(*) from triggers t,hosts h,items i,functions f  where t.istrue=1 and f.itemid=i.itemid and h.hostid=i.hostid and t.triggerid=f.triggerid and i.status in (0,2) $cond group by 1");
		}
		else
		{
			$result=DBselect("select t.priority,count(*) from triggers t,hosts h,items i,functions f  where f.itemid=i.itemid and h.hostid=i.hostid and t.triggerid=f.triggerid and i.status in (0,2) $cond group by 1");
		}
		$p0=$p1=$p2=$p3=$p4=$p5=0;
		for($i=0;$i<DBnum_rows($result);$i++)
		{
			$priority=DBget_field($result,$i,0);
			$count=DBget_field($result,$i,1);
			if($priority==0) $p0=$count;
			if($priority==1) $p1=$count;
			if($priority==2) $p2=$count;
			if($priority==3) $p3=$count;
			if($priority==4) $p4=$count;
			if($priority==5) $p5=$count;
		}
		echo "\n<TABLE BORDER=0 COLS=6 WIDTH=\"100%\" BGCOLOR=\"#CCCCCC\" cellspacing=1 cellpadding=0>";
		echo "<TR ALIGN=CENTER>";
		echo "<TD><B>Not classified: $p0</B></TD>";
		echo "<TD><B>Just information: $p1</B></TD>";
		echo "<TD><B>Warning: $p2</B></TD>";
		echo "<TD BGCOLOR=#DDAAAA><B>Average: $p3</B></TD>";
		echo "<TD BGCOLOR=#FF8888><B>High: $p4</B></TD>";
		echo "<TD BGCOLOR=RED><B>Disaster: $p5</B></TD>";
		echo "</TR>";
		echo "</TABLE>";
	}
	else
	{
		show_table_header("<A HREF=\"tr_status.html?onlytrue=$onlytrue&noactions=$noactions&compact=$compact&fullscreen=1\">TRIGGERS $time</A>");
	}
  
	echo "\n<TABLE BORDER=0 COLS=5 WIDTH=\"100%\" BGCOLOR=\"#CCCCCC\" cellspacing=1 cellpadding=3>";
	echo "<TR ALIGN=CENTER>";
	echo "<TD ALIGN=LEFT><B>Description";
	if($compact!='true') {echo "<BR><FONT SIZE=-1>Expression</FONT></B>";}
	echo "</TD>";
	echo "<TD WIDTH=\"5%\"><B>Status</B></TD>";
	echo "<TD WIDTH=\"9%\"><B>Priority</B></TD>";
	if($noactions=='true')
		echo "<TD WIDTH=\"12%\">";
	else
		echo "<TD WIDTH=\"5%\">";

	echo "<B>Last change";
	echo "</TD>";
   
	if($noactions!='true')
	{  
		echo "<TD WIDTH=\"8%\" NOSAVE><B>Actions</B></TD>";
	}
	echo "<TD WIDTH=\"9%\"><B>Comments</B></TD>";
	echo "</TR>\n";

	if(isset($hostid))
	{
		$cond=" and h.hostid=$hostid ";
	}
	else
	{
		$cond="";
	}


	if($onlytrue=='true')
	{
		$result=DBselect("select distinct t.triggerid,t.istrue,t.description,t.expression,t.priority,t.lastchange,t.comments from triggers t,hosts h,items i,functions f  where t.istrue=1 and f.itemid=i.itemid and h.hostid=i.hostid and t.triggerid=f.triggerid and i.status in (0,2) $cond order by t.priority desc, t.description");
	}
	else
	{
		$result=DBselect("select distinct t.triggerid,t.istrue,t.description,t.expression,t.priority,t.lastchange,t.comments from triggers t,hosts h,items i,functions f  where f.itemid=i.itemid and h.hostid=i.hostid and t.triggerid=f.triggerid and i.status in (0,2) $cond order by t.priority desc, t.description");
	}
	$col=0;
	for($i=0;$i<DBnum_rows($result);$i++)
	{
		$triggerid=DBget_field($result,$i,0);

// Check for dependencies

		$sql="select count(*) from trigger_depends d, triggers t where d.triggerid_down=$triggerid and d.triggerid_up=t.triggerid and t.istrue=1";
		$result2=DBselect($sql);

		if(DBget_field($result2,0,0)>0)
		{
			continue;
		}


		if($col==1)
		{
			echo "\n<TR BGCOLOR=#EEEEEE>";
			$col=0;
		} else
		{
			echo "\n<TR BGCOLOR=#DDDDDD>";
			$col=1;
		}
		$expression=DBget_field($result,$i,3);
		$description=DBget_field($result,$i,2);
		$istrue=DBget_field($result,$i,1);
		$priority=DBget_field($result,$i,4);
		$lastchange=DBget_field($result,$i,5); 
		$comments=DBget_field($result,$i,6);
 
		$lastchange=date("d M H:i:s",$lastchange);

		echo "<TD>";

		echo "$description";

		if($compact!='true')
		{
			$expression=explode_exp($expression,1);
			echo "<BR><FONT COLOR=\"#000000\" SIZE=-2>$expression</FONT>";
		}
		echo "</TD>";
		if($istrue==0)
		{ echo "<TD ALIGN=CENTER><FONT COLOR=\"00AA00\">FALSE</FONT></TD>";}
		elseif($istrue==2)
		{ echo "<TD ALIGN=CENTER><FONT COLOR=\"777777\">DISABLED</FONT></TD>";  }
		else 
		{  echo "<TD ALIGN=CENTER><FONT COLOR=\"AA0000\">TRUE</FONT></TD>"; }

		if($priority==0)	echo "<TD ALIGN=CENTER>Not classified</TD>";
		elseif($priority==1)	echo "<TD ALIGN=CENTER>Just information</TD>";
		elseif($priority==2)	echo "<TD ALIGN=CENTER>Warning</TD>";
		elseif($priority==3)	echo "<TD ALIGN=CENTER BGCOLOR=#DDAAAA>Average</TD>";
		elseif($priority==4)	echo "<TD ALIGN=CENTER BGCOLOR=#FF8888>High</TD>";
		elseif($priority==5)	echo "<TD ALIGN=CENTER BGCOLOR=RED>Disaster !!!</TD>";
		else			echo "<TD ALIGN=CENTER><B>$priority</B></TD>";

		echo "<TD ALIGN=CENTER><A HREF=\"alarms.html?triggerid=$triggerid\">$lastchange</a>";
		echo "</TD>";

		if($noactions!='true')
		{
			echo "<TD>";
			echo "<A HREF=\"actions.html?triggerid=$triggerid\">Show actions</A> - ";
			echo "<A HREF=\"alarms.html?triggerid=$triggerid\">History</A> - ";
			if(isset($hostid))
			{
				echo "<A HREF=\"triggers.html?hostid=$hostid&triggerid=$triggerid#form\">Change</A>";
			}
			else
			{
				echo "<A HREF=\"triggers.html?triggerid=$triggerid#form\">Change</A>";
			}
			echo "</TD>";
		}
		if($comments!="")
		{
			echo "<TD ALIGN=CENTER><A HREF=\"tr_comments.html?triggerid=$triggerid\">Show</a></TD>";
		}
		else
		{
			echo "<TD ALIGN=CENTER><A HREF=\"tr_comments.html?triggerid=$triggerid\">Add</a></TD>";
		}
		if($istrue==0)	echo "</TR>\n";
	}
	echo "</TABLE>\n";

	$i=DBnum_rows($result);

	show_table_header("Total:$i");
?>

<?
	show_footer();
?>
